package com.zis.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.inter.IUserInter;
import com.zis.system.inter.IUserRoleInter;
import com.zis.system.model.User;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Create by wangshen 2018/1/31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserInter userInter;

    @Autowired
    private IUserRoleInter userRoleInter;

    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public PageQuery<User> findByPage(@RequestBody SearchParams params) throws Exception {
        return userInter.findByPage(params);
    }

    /**
     * 条件查询
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    public ResponseEntity findByCondition(@RequestBody Map params) throws Exception {
        return userInter.findByCondition(params);
    }

    /**
     * 新增
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseEntity insert(@RequestBody User model) throws Exception {
        return userInter.insert(model);
    }

    /**
     * 修改
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseEntity update(@RequestBody User model) throws Exception {
        return userInter.update(model);
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ResponseEntity delete(@PathVariable("id") String id) throws Exception {
        return userInter.delete(id);
    }

    /**
     * 主键查询
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.GET)
    public ResponseEntity selectById(@PathVariable("id") String id) throws Exception {
        return userInter.selectById(id);
    }

    /**
     * 根据用户名查询用户信息
     * @param loginName
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/select/username/{loginName}", method = RequestMethod.GET)
    public ResponseEntity findByLoginName(@PathVariable("loginName") String loginName) throws Exception {
        return userInter.findByLoginName(loginName);
    }

    /**
     * 用户角色保存
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/role/save", method = RequestMethod.POST)
    public ResponseEntity userRoleSave(@RequestBody JSONObject params) throws Exception {
        return userRoleInter.userRoleSave(params);
    }

    /**
     * 根据userId查询用户角色
     * @param userId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/select/role/{userId}", method = RequestMethod.GET)
    public ResponseEntity findByUserId(@PathVariable("userId") String userId) throws Exception {
        return userRoleInter.findByUserId(userId);
    }
}
